package sddz.foodmanage.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoLinearLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Request;
import sddz.foodmanage.R;
import sddz.foodmanage.activity.AboutUsActivity;
import sddz.foodmanage.activity.ChangePasswordActivity;
import sddz.foodmanage.activity.LoginActivity;
import sddz.foodmanage.activity.MainActivity;
import sddz.foodmanage.base.BaseFragment;
import sddz.foodmanage.model.ResultBean;
import sddz.foodmanage.model.UserStatusBean;
import sddz.foodmanage.utils.API;
import sddz.foodmanage.utils.HttpUtils;
import sddz.foodmanage.utils.ImageLoader;
import sddz.foodmanage.utils.SpfUtils;

/**
 * @Date:2019/7/3
 * @Author:lbb 我的
 */
public class MyFragment extends BaseFragment {


    @BindView(R.id.title_navigation_icon)
    ImageView titleNavigationIcon;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.title_menu_icon)
    ImageView titleMenuIcon;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.image_title_right)
    ImageView imageTitleRight;
    @BindView(R.id.iv_person)
    ImageView ivPerson;
    @BindView(R.id.tv_person_name)
    TextView tvPersonName;
    @BindView(R.id.linear_family_changepassword)
    AutoLinearLayout linearFamilyChangepassword;
    @BindView(R.id.linear_family_about)
    AutoLinearLayout linearFamilyAbout;
    @BindView(R.id.btn_exit)
    Button btnExit;
    Unbinder unbinder;

    public MyFragment() {
        // Required empty public constructor
    }
    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        view.setClickable(true);
        return view;
    }

    private void initView() {
        titleNavigationIcon.setVisibility(View.GONE);
        titleText.setText("我的");
        tvPersonName.setText(SpfUtils.getUserData(mActivity).getName());
        ImageLoader.loadRoundImageUrl(mActivity, SpfUtils.getUserData(mActivity).getAvatar(), ivPerson);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.linear_family_changepassword, R.id.linear_family_about, R.id.btn_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_family_changepassword:// 修改密码
                startActivity(new Intent(mActivity, ChangePasswordActivity.class));
                break;
            case R.id.linear_family_about://关于我们
                startActivity(new Intent(mActivity, AboutUsActivity.class));
                break;
            case R.id.btn_exit://退出登录
                exitlogin();
                break;
        }
    }
    private void exitlogin(){
        HttpUtils.getInstance().POST(mActivity, API.getexitLogin, JSON.toJSONString(""),SpfUtils.gettoken(mActivity),new HttpUtils.OnOkHttpCallback() {
            @Override
            public void onSuccess(String body) {
                Log.e("退出登录", body);
                if (!TextUtils.isEmpty(body)) {
                    try {
                        JSONObject jsonObject = new JSONObject(body);
                        int code= jsonObject.optInt("status_code");
                        if(!jsonObject.optString("message").equals("")) {
                            show(jsonObject.optString("message"));
                        }
                        if(code==200){
                            SpfUtils.setLogin(mActivity, false);
                            SpfUtils.clearUserData(mActivity);
                            SpfUtils.clearwaitpl(mActivity);
                            Log.e("清空后", JSON.toJSONString(SpfUtils.getUserData(mActivity)));
                            startActivity(new Intent(mActivity, LoginActivity.class));
                            mActivity.finish();
                        }else if(code==210){
                            String token=jsonObject.optJSONObject("data").optString("token");
                            Log.e("33333333", token);
                            SpfUtils.savetoken(mActivity,token);
                            exitlogin();
                        }else if(code==401){
                            SpfUtils.setLogin(mActivity, false);
                            startActivity(new Intent(mActivity, LoginActivity.class));
                            mActivity.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Request error, Exception e) {
                Log.e("ssss", e.toString());
                show("访问数据失败，请稍后重试");
            }
        });
    }
}
